package com.atcx.service.impl;

import com.atcx.mapper.AchievementMapper;
import com.atcx.mapper.ClassesMapper;
import com.atcx.mapper.StudentClassMapper;
import com.atcx.mapper.UserMapper;
import com.atcx.pojo.Achievement;
import com.atcx.pojo.Classes;
import com.atcx.pojo.StudentClass;
import com.atcx.pojo.User;
import com.atcx.service.ClassesService;
import com.atcx.util.PageResult;
import com.atcx.util.QueryPageBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pangShu
 * @since 2022-03-23
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {


    @Resource
    private StudentClassMapper studentClassMapper;
    @Resource
    private AchievementMapper achievementMapper;
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public int addClasses(Classes classes) {
        //,根据用户ID设置班主任
        User user = userMapper.selectById(classes.getUserid());
        classes.setTeacher(user.getName());
        classes.setNumber(0);
        classes.setAscore(new BigDecimal(0));
        int insert = classesMapper.insert(classes);
        return insert;
    }

    @Override
    public int updateClasses(Classes classes) {
        User user = userMapper.selectById(classes.getUserid());
        classes.setTeacher(user.getName());
        int i = classesMapper.updateById(classes);
        return i;
    }

    @Override
    public int deleteClasses(int id) {
        int i = classesMapper.deleteById(id);
        return i;
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        //先计算班级平均成绩
        computer();
        //计算各个科目最高成绩
        setScore();
        Page<Classes> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<Classes> wrapper = new QueryWrapper<>();
        if (queryPageBean.getQueryString() != null&&queryPageBean.getQueryString().trim().length()!=0){
            //有查询条件就添加查询条件
            String trim = queryPageBean.getQueryString().trim();
            wrapper.eq("teacher",trim);
        }
        Page<Classes> classesPage = classesMapper.selectPage(page, wrapper);
        PageResult pageResult = new PageResult(classesPage.getTotal(), classesPage.getRecords());
        return pageResult;
    }

    @Override
    public Map<String, Object> getTotal() {
        Map<String,Object> map = new HashMap<>();
        List<String> classNames = new ArrayList<>();
        List<BigDecimal> totals = new ArrayList<>();
        List<Classes> classes = classesMapper.selectList(new QueryWrapper<Classes>());
        for (Classes aClass : classes) {
            classNames.add(aClass.getClassname());
            totals.add(aClass.getAscore());
        }
        map.put("classNames",classNames);
        map.put("totals",totals);

        return map;
    }

    @Override
    public List<Classes> getList() {
        List<Classes> list = classesMapper.selectList(new QueryWrapper<Classes>());
        return list;
    }

    @Override
    public Classes getClassesById(int id) {
        Classes classes = classesMapper.selectById(id);
        return classes;
    }

    //班级人数大于2重新计算总成绩
    public void computer(){

        List<Classes> classes = classesMapper.selectList(new QueryWrapper<Classes>());

        for (Classes aClass : classes) {
            //班级人数大于2
            QueryWrapper<StudentClass> wrapper = new QueryWrapper<>();
            wrapper.eq("claId",aClass.getClassid());
            List<StudentClass> studentClasses = studentClassMapper.selectList(wrapper);
            if (studentClasses.size()>1){
                int j = 0;
                for (StudentClass studentClass : studentClasses) {
                    Achievement achievement = achievementMapper.selectOne(new QueryWrapper<Achievement>().eq("userId", studentClass.getUserid()));
                    if (achievement==null){
                        break;
                    }
                    j += achievement.getTotal().intValue();
                    System.out.println(j);
                }

                aClass.setAscore(new BigDecimal(j).divide(new BigDecimal(studentClasses.size()),3,BigDecimal.ROUND_HALF_DOWN));
                classesMapper.updateById(aClass);
            }
            }


        }
        //为班级设置各科目最高成绩
        public void setScore(){
            //1.先判断是否设置过成绩
            List<Classes> list = classesMapper.selectList(new QueryWrapper<Classes>());
            for (Classes classes : list) {
                if (classes.getNumber()!=0){
                    if (classes.getMath()==null){
                        //可以赋值
                        //升序查询各个科目成绩
                        //数学
                        System.out.println(classes);
                        QueryWrapper<Achievement> queryWrapper1 = new QueryWrapper<>();
                        queryWrapper1.eq("classId",classes.getClassid());
                        queryWrapper1.orderByDesc("math");
                        List<Achievement> achievements = achievementMapper.selectList(queryWrapper1);
                        System.out.println(achievements);
                        classes.setMath(achievements.get(0).getMath());
                        //计算机
                        QueryWrapper<Achievement> queryWrapper2 = new QueryWrapper<>();
                        queryWrapper2.eq("classId",classes.getClassid());
                        queryWrapper2.orderByDesc("computer");
                        List<Achievement> achievements1 = achievementMapper.selectList(queryWrapper2);
                        classes.setComputer(achievements1.get(0).getComputer());
                        //前端
                        QueryWrapper<Achievement> queryWrapper3 = new QueryWrapper<>();
                        queryWrapper3.eq("classId",classes.getClassid());
                        queryWrapper3.orderByDesc("web");
                        List<Achievement> achievements2 = achievementMapper.selectList(queryWrapper3);
                        classes.setWeb(achievements1.get(0).getWeb());
                        //英语
                        QueryWrapper<Achievement> queryWrapper4 = new QueryWrapper<>();
                        queryWrapper4.eq("classId",classes.getClassid());
                        queryWrapper4.orderByDesc("english");
                        List<Achievement> achievements3 = achievementMapper.selectList(queryWrapper4);
                        classes.setEnglish(achievements3.get(0).getEnglish());
                        //c语言
                        QueryWrapper<Achievement> queryWrapper5 = new QueryWrapper<>();
                        queryWrapper5.eq("classId",classes.getClassid());
                        queryWrapper5.orderByDesc("clanguage");
                        List<Achievement> achievements4 = achievementMapper.selectList(queryWrapper5);
                        classes.setClanguage(achievements4.get(0).getClanguage());
                        //设置班级成绩
                        classesMapper.updateById(classes);
                    }
                }

            }
        }
}
